package com.example.knowledgeget1.ChatController;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.CompletableFuture;


import com.example.knowledgeget1.ChatCommon.Request.ChatRequest;
import com.example.knowledgeget1.ChatCommon.Request.ChatRequestData;
import com.example.knowledgeget1.ChatCommon.Response.UserChatResponse;
import com.example.knowledgeget1.ChatCommon.Service.ChatService;
import com.example.knowledgeget1.ChatCommon.Service.NodeTypeService;
import com.example.knowledgeget1.Common.ResponseResult;
import com.example.knowledgeget1.Util.NodeResponseLexer;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class ChatController {
    @Autowired
    private ChatService chatService;
    @Autowired
    private TaskExecutor taskExecutor1;

    @PostMapping("/chat")
    public ResponseResult chat(@RequestBody Map<String, String> request) {
        CompletableFuture.runAsync(() -> {
            try {
                chatService.handleChatRequest(request.get("courseName"), request.get("knowName"));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }, taskExecutor1);
        return ResponseResult.success("知识点构建中");
    }

    @PostMapping("/usrchat")
    public ResponseResult userChat(@RequestBody Map<String, String> request) throws Exception {
        String userId = request.get("userId");
        String userMessage = request.get("message");

        return ResponseResult.success(chatService.userChat(userId, userMessage));
    }
}
